<?php
/**
 * Created by PhpStorm.
 * User: jim
 * Date: 2016-12-19
 * Time: 10:29
 */

namespace App\Http\Controllers\chat;


use App\Http\Controllers\Controller;
use App\Models\Doctor;
use App\Models\Message;
use App\Models\Order;
use App\Models\User_info;
use App\Models\Order_info;
use App\Models\Order_time;
use App\Models\Sender;
use Illuminate\Http\Request;

class DoctorController extends Controller
{
    public function index()
    {
        $ob = new Doctor();
        $list = $ob->get();
        return view("chat.doctor", ["list" => $list]);
    }

    /** 药师登录后台 */
    public function login()
    {
        return view("chat.login");
    }

    /** 查询咨询记录并渲染药师工作台视图 */
    public function asklist()
    {
        $ob = new Message();
        $rid = session()->get('docuser')->user_id;
        $list = $ob->select('sender_id')->where('receiver_id', $rid)->orderBy('created_at', 'desc')->distinct()->get();
        $listu = [];
        foreach ($list as $k => $v) {
            $listu2 = $ob->select('message.sender_id', 'message.receiver_id', 'message.content', 'message.created_at', 'user_info.nickname', 'user_info.photo')
                ->join('user_info', 'message.sender_id', '=', 'user_info.user_id')
                ->where('message.sender_id', $v->sender_id)
                ->where('message.receiver_id', $rid)
                ->orderBy('message.created_at', 'desc');
            $listu[] = $listu2->first();
        }
        return view("chat.asklist", ['list' => $listu]);
    }

    /** Ajax实时查询咨询记录 return json */
    public function askinfo()
    {
        $ob = new Message();
        $obu = new User_info();
        $rid = session()->get('docuser')->user_id;
        $list = $ob->select('sender_id')->where('receiver_id', $rid)->orderBy('created_at', 'desc')->distinct()->get();

        $arr = [];
        foreach ($list as $k => $v) {

            $obb = $ob->select('message.sender_id', 'message.receiver_id', 'message.content', 'message.created_at', 'user_info.nickname', 'user_info.photo')
                ->join('user_info', 'message.sender_id', '=', 'user_info.user_id')
                ->where('message.sender_id', $v->sender_id)
                ->where('message.receiver_id', $rid)
                ->orderBy('message.created_at', 'desc');
            $listu1 = $ob->select('message.sender_id', 'message.receiver_id', 'message.content', 'message.created_at', 'user_info.nickname', 'user_info.photo')
                ->join('user_info', 'message.sender_id', '=', 'user_info.user_id')
                ->where('message.sender_id', $v->sender_id)
                ->where('message.receiver_id', $rid)
                ->where("message.status", 1)
                ->orderBy('message.created_at', 'desc');
            if ($count = $listu1->get()->count()) {

            } else {
                $count = 0;
            }
            $obb2 = $obb->first();
            $time = strtotime($obb2->created_at);
            $time = $time . "000";
            $arr[] = ["sender_id" => $v->sender_id, "count" => $count, "nickname" => $obb2->nickname, "time" => $time, "content" => $obb2->content, "photo" => $obb2->photo];
            $str = json_encode($arr);
        }

        return $str;
    }

    /** 更改药师工作状态 */
    public function csta(Request $request)
    {
        $status = $request->input('status');
        $id = session('docuser')->id;
        $res = Doctor::where('id', $id)->get();
        $ress = Doctor::where('id', $id)->update(['status' => $status]);

        return $ress;
    }

    /** Ajax查询药师信息 */
    public function selt()
    {
        $id = session('docuser')->id;
        $ob = Doctor::where('id', $id)->get();
        $str = $ob->toJson();
        $res = Order::where('doctor_id', $id)->get()->count();
        $strr = "," . '"order":' . $res;
        //拼接json
        $n = strlen($str) - 2;
        $start = substr($str, 0, $n);
        $end = substr($str, $n);
        $strs = ($start . $strr . $end);
        return $strs;
    }

    /** 查询药师信息并渲染修改视图 */
    public function docinfo()
    {
        $id = session('docuser')->id;
        $list = Doctor::where('id', $id)->first();
        //dd($list);
        return view('chat.docinfo', ['list' => $list]);
    }

    /** 修改药师信息并返回 */
    public function upinfo(Request $request)
    {
        $od = new Doctor();
        $id = session('docuser')->id;
        $m = $od->upinfo($request, $id);
        return back()->with('msg','个人信息修改成功');
    }

    public function count()
    {
        $id = session('docuser')->id;
        $res = Order::where('doctor_id', $id)->get()->count();
        return count;
    }

    /** 封装订单数据接口 */
    public function redata()
    {
        $res = Order::
        select("order.*", "order_time.pay_at")
            ->join('order_time', 'order.id', '=', 'order_time.order_id')
            ->where([['status', 2], ['state', 2]])
            ->get();
        $json1 = '';
        $json = '';
        foreach ($res as $v) {
            $list = Order_info::where('order_id', $v->id)->get();
            $json .= ',"order":' . json_encode($v) . ',' . '"order_info":' . json_encode($list);
            $json1 .= ",{" . ltrim($json, ',') . "}";
        }
        $json = "[" . ltrim($json1, ',') . "]";
        return $json;
    }

    /** 修改订单状态 */
    public function chstu(Request $request)
    {
        $action = $request->input('action');
        $did = session('docuser')->id;
        $id = $request->input('id');
        $time = date('Y-m-d H:i:s');
        if ($action == 2) {
            $res = Order::where('id', $id)->update(['state' => '3', 'doctor_id' => $did]);
            $ress = Order_time::where('order_id', $id)->update(['doctor_at' => $time]);
            return $res + $ress;
        } else if ($action == 1) {
            $res = Order::where('id', $id)->update(['status' => '6', 'state' => '5', 'doctor_id' => $did]);
            $ress = Order_time::where('order_id', $id)->update(['doctor_at' => $time, 'complete_at' => $time]);
            return $res + $ress;
        }

    }

    /** 查询药师信息并渲染修改视图 */
    public function credinfo()
    {
        $id = session('docuser')->id;
        $list = Doctor::where('id', $id)->first();
        return view('chat.credinfo', ['list' => $list]);
    }

    public function upcred(Request $request)
    {
        $od = new Doctor();
        $id = session('docuser')->id;
        $m = $od->upcred($request, $id);
        return back()->with('msg','个人信息修改成功');
    }

}